package cn.wind.xboot.conf.config;

import org.dozer.DozerBeanMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.util.Arrays;

/**
 * <p>Title: DbConfiguration</p>
 * <p>Description: TODO</p>
 *
 * @author xukk
 * @version 1.0
 * @date 2018/6/19
 */
@Configuration
@AutoConfigureAfter(XbootConfiguration.class)
@MapperScan(value = {"cn.wind.xboot.**.dao"})
public class InitConfiguration {
    @Autowired
    private XbootConfiguration xbootConfiguration;

    @Bean
    public DozerBeanMapper mapper() {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<mappings xmlns=\"http://dozer.sourceforge.net\"\n" +
                "          xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "          xsi:schemaLocation=\"http://dozer.sourceforge.net\n" +
                "          http://dozer.sourceforge.net/schema/beanmapping.xsd\">\n" +
                "    <configuration>\n" +
                "        <date-format>yyyy-MM-dd HH:mm:ss</date-format>\n" +
                "        <map-null>false</map-null>\n" +
                "        <map-empty-string>true</map-empty-string>\n" +
                "    </configuration>\n" +
                "</mappings>";
        DozerBeanMapper mapper = new DozerBeanMapper();
        mapper.addMapping(new ByteArrayInputStream(xml.getBytes()));
        mapper.setMappingFiles(Arrays.asList(xbootConfiguration.getDozer().getBasename()));
        return mapper;
    }
}
